// components/verifyCode/verifyCode.js
const HTTP = require('../../utils/util');
const Common =  require('../../libs/common');
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        phone: {
            value: '',
            type: Number
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        text: '发送验证码',
        second: 60,
        active: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleTap() {
            console.log('发送验证码');
            if(Common.checkPhone(this.properties.phone)){
                this.getVerifyCode();
            }
        },
        getVerifyCode() {
            if (!this.data.active) {
                HTTP.postData('/api/auth/sendMsg', {
                    mobile: Number(this.properties.phone)
                }, res => {
                    console.log(res);
                    this.somethingHappen();
                });
            }else {
                wx.showToast({
                    icon:'none',
                    title:'请在'+this.data.second+'秒后重试！'
                });
            }
        },
        somethingHappen() {
            this.setData({
                active: true,
                text: this.data.second + 's'
            });
            let second = this.data.second;
            let timeId = setInterval(()=>{
                if(second <= 0){
                    this.setData({
                        active:false,
                        second:60,
                        text: '发送验证码'
                    });
                    clearInterval(timeId);
                    return false;
                }else {
                    second = second -1;
                    this.setData({
                        second:second,
                        text:second+'s'
                    });
                }
            },1000)
        }
    }
})
